package com.Aug.controller.user;

import com.Aug.entity.User;
import com.Aug.service.UserService;
import com.Aug.util.Page;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;




//异步刷新处理用户列表
@WebServlet("/user2")
public class User2Servlet extends HttpServlet {

    UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer currentPage = req.getParameter("currentPage") == null ? 1
                : Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = req.getParameter("pageSize") == null ? 2
                : Integer.parseInt(req.getParameter("pageSize"));



        //查询所有用户
        List<User> users = userService.findAllUsers(currentPage,pageSize);
        Integer total = userService.getUserTotal();



        //转发user.jsp
        Page<User> page = new Page<>(total,users);
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        //给前端传过去
        String pageStr = JSONObject.toJSONString(page);
        System.out.println(pageStr);
        resp.setHeader("Conten-Type","application/json;charset=utf-8");
        resp.getWriter().write(pageStr);
        resp.getWriter().flush();

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/pages/user/user2.jsp").forward(req,resp);
    }
}
